package com.fy.tt.service.impl;

import com.fy.tt.action.TemplateTool;
import com.fy.tt.dao.WordDao;
import com.fy.tt.entity.Word;
import com.fy.tt.entity.query.SemanticQuery;
import com.fy.tt.entity.query.WordQuery;
import com.fy.tt.entity.vo.WordAndSemanticVo;
import com.fy.tt.entity.vo.WordVo;
import com.fy.tt.service.WordService;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class WordServiceImpl implements WordService {
    Connection dbConnection = null;
    WordDao wordDao = null;

    {
        try {
            dbConnection = TemplateTool.getDBConnection();
            wordDao = TemplateTool.getBean(WordDao.class);

        } catch (Exception e) {
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }

    @Override
    public List<WordVo> getAllWords() {
        List<WordVo> words = null;
        try {
            words = wordDao.getAllWords(dbConnection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return words;
    }

    @Override
    public int getTotalWord(WordQuery wordQuery) {
        int count = -1;
        try {
            count = wordDao.getTotalWord(dbConnection, wordQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int addWord(Word word) {
        int i = -1;
        try {
            wordDao.save(dbConnection, word);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int deleteWord(int id) {
        int i = -1;
        try {
            i = wordDao.deleteWord(dbConnection, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateWord(Word word) {
        int i = -1;
        try {
            i = (int) wordDao.updateIgnoreNull(dbConnection, word);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<Word> getPageWords(int pageSize, int current) {
        List<Word> words = null;
        try {
            words = wordDao.getPageWords(dbConnection, pageSize, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return words;
    }

    @Override
    public List<WordVo> getConditionPageWords(WordQuery wordQuery, int pageSize, int current) {
        List<WordVo> words = null;
        try {
            words = wordDao.getConditionPageWords(dbConnection, wordQuery, pageSize, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return words;
    }

    @Override
    public List<WordAndSemanticVo> getWordSemantic() {
        List<WordAndSemanticVo> wordAndSemanticVos = null;
        try {
            wordAndSemanticVos = wordDao.getWordSemantic(dbConnection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return wordAndSemanticVos;
    }

    @Override
    public List<WordAndSemanticVo> getAllWordSemantic() {
        List<WordAndSemanticVo> wordAndSemanticVos = null;
        try {
            wordAndSemanticVos = wordDao.getAllWordSemantic(dbConnection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return wordAndSemanticVos;
    }

    @Override
    public List<WordAndSemanticVo> getConditionPageAllWordSemantic(SemanticQuery semanticQuery, int pageSize, int current) {
        List<WordAndSemanticVo> wordAndSemanticVos = null;
        try {
            wordAndSemanticVos = wordDao.getConditionPageAllWordSemantic(dbConnection, semanticQuery, pageSize, current);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return wordAndSemanticVos;
    }

    @Override
    public List<WordVo> getLastSaveWord() {
        List<WordVo> allWords = this.getAllWords();
        List<WordVo> collect = allWords.stream().sorted((a, b) -> {
            return (int) (b.getUpdateTime().getTime() - a.getUpdateTime().getTime());
        }).limit(5L).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<WordAndSemanticVo> getConditionPageWordSemanticDeveloped(SemanticQuery semanticQuery, Integer pageSize, Integer current) {
        List<WordAndSemanticVo> wordAndSemanticVos = null;
        try {
            wordAndSemanticVos = wordDao.getConditionPageWordSemanticDeveloped(dbConnection, semanticQuery, pageSize, current);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return wordAndSemanticVos;
    }
}
